package com.es.elasticsearch.controller;

import com.es.elasticsearch.model.User;
import com.es.elasticsearch.util.ESIndexUtil;
import com.es.elasticsearch.util.ESMapperUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;

@RestController
public class ESMapperController {

    /**
     * 新增文档
     * @param indexName
     * @return
     * @throws IOException
     */
    @GetMapping("/es/mapper/add/{index}")
    public String add(@PathVariable(value = "index") String indexName) throws IOException {
        if (StringUtils.isBlank(indexName)) {
            return "Error : indexName不能为空";
        }

        // 创建数据对象
        User user = new User();
        user.setName("zhangsan");
        user.setAge(30);
        user.setSex("男");

        String indexID = "1001";

        return  ESMapperUtil.addMapper(indexName,user,indexID);
    }



    /**
     * 批量新增文档
     * @param indexName
     * @return
     * @throws IOException
     */
    @GetMapping("/es/mapper/addBatch/{index}")
    public String addBatch(@PathVariable(value = "index") String indexName) throws IOException {
        if (StringUtils.isBlank(indexName)) {
            return "Error : indexName不能为空";
        }

        // 创建数据对象
        User user = new User();
        user.setName("zhangsan");
        user.setAge(30);
        user.setSex("男");
        user.setId("1001");

        User user1 = new User();
        user1.setName("lisi");
        user1.setAge(18);
        user1.setSex("女");
        user1.setId("1002");

        ArrayList<User> arrayList = new ArrayList<>();
        arrayList.add(user);
        arrayList.add(user1);

        String indexID = "1001";

        return  ESMapperUtil.addBatchMapper(indexName,arrayList,indexID);
    }


    /**
     * 修改文档
     * @param indexName
     * @return
     * @throws IOException
     */
    @GetMapping("/es/mapper/update/{index}")
    public String update(@PathVariable(value = "index") String indexName) throws IOException {
        if (StringUtils.isBlank(indexName)) {
            return "Error : indexName不能为空";
        }

        // 创建数据对象
        User user = new User();
        user.setSex("女");

        String indexID = "1001";

        return  ESMapperUtil.addMapper(indexName,user,indexID);
    }




    /**
     * 查询文档
     * @param indexName
     * @return
     * @throws IOException
     */
    @GetMapping("/es/mapper/query/{index}")
    public String query(@PathVariable(value = "index") String indexName) throws IOException {
        if (StringUtils.isBlank(indexName)) {
            return "Error : indexName不能为空";
        }


        String indexID = "1001";

        return  ESMapperUtil.queryMapper(indexName,indexID);
    }

    /**
     * 查询文档
     * @param indexName
     * @return
     * @throws IOException
     */
    @GetMapping("/es/mapper/delete/{index}")
    public String delete(@PathVariable(value = "index") String indexName) throws IOException {
        if (StringUtils.isBlank(indexName)) {
            return "Error : indexName不能为空";
        }

        String indexID = "1001";

        return  ESMapperUtil.queryMapper(indexName,indexID);
    }



}
